var commonUtil = require("../../../utils/commonUtil.js")
var globalData = getApp().globalData;
var httpsUrl = globalData.httpsUrl;
var imgUrl = globalData.imgUrl;
Page({

  data: {
    imgUrl: imgUrl,
    typeFlag:0,//0收入 1支出
    titleFlag: -1,//全部就不传 0:待打款 1:已打款
    list:[],//
    orderId:'',
    openid:'',
    params:[],
    //支出类型 expandType 1保养  2维修  3 维修
  },

  loadData:function(){
    var params = new Object();
    params.page = 0;
    params.rows = 20;
    let status = this.data.titleFlag;
    if (status != -1) {
      params.status = status;
    } else {
      params.status = '';
    }
    this.setData({params:params})
    console.log("params:" + JSON.stringify(params));
    let typeFlag = this.data.typeFlag;
    if (typeFlag == 0){
      this.searchList();
    }else{
      this.expandList();//支出
    }
  },
  searchList:function(){
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    commonUtil.showLoading("加载中...");
    wx.request({
      url: httpsUrl + "/income/list",
      // url: httpsUrl + "/income/list?status=0&page=0&rows=10",
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      dataType: "json",
      method: "GET",
      data: that.data.params,
      success: function (res) {
        commonUtil.hideLoading();
        res = res.data;
        console.info("返回："+JSON.stringify(res));
        console.log("res.code:"+JSON.stringify(res.code));
        if (res.code == 0) {
          var rows = res.data;
          if (rows != null && rows.length > 0) {
            for (let i = 0; i < rows.length; i++) {
              if (!commonUtil.isEmpty(rows[i].realPayMoney)) {
                rows[i].realPayMoney = rows[i].realPayMoney.toFixed(2);
              }
            }
          }
          that.setData({
            list: rows
          })
        } else {
          commonUtil.showModal("订单加载失败，请稍候重试：" + res.message);
        }
      }
    })
  },
  expandList: function () {
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    commonUtil.showLoading("加载中...");
    wx.request({
      url: httpsUrl + "/expand/list",
      // url: httpsUrl + "/income/list?status=0&page=0&rows=10",
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      dataType: "json",
      method: "GET",
      data: that.data.params,
      success: function (res) {
        commonUtil.hideLoading();
        res = res.data;
        console.info("expandList返回：" + JSON.stringify(res));
        console.log("res.code:" + JSON.stringify(res.code));
        if (res.code == 0) {
          var rows = res.data;
          if (rows != null && rows.length > 0) {
            for (let i = 0; i < rows.length; i++) {
              if (!commonUtil.isEmpty(rows[i].realPayMoney)) {
                rows[i].realPayMoney = rows[i].realPayMoney.toFixed(2);
              }
            }
          }
          that.setData({
            list: rows
          })
        } else {
          commonUtil.showModal("订单加载失败，请稍候重试：" + res.msg);
        }
      }
    })
  },
  //切换收入支出
  selectType: function (e) {
    let id = e.currentTarget.id;
    this.setData({ typeFlag: id})
    this.loadData();
  },
  //设置显示块id
  titleSelect: function (e) {
    let id = e.currentTarget.id;
    this.setData({
      titleFlag: id,
    })
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})